
/*
 * @: 管道
 * @Version: 1.0
 * @Autor: ziwei
 * @Date: 2021-11-02 15:47:08
 * @LastEditors: ziwei
 * @LastEditTime: 2021-11-05 16:15:09
 */
function createChannel() {
  let currentTakers = [];//listeners
  function once(actionType,taker) {//take
      taker.type = actionType;
      taker.cancel = () => currentTakers = currentTakers.filter(item=>item!==taker)
      currentTakers.push(taker);
  }
  function emit(action) {//put
      currentTakers.forEach(taker=>{
          if(taker.type = action.type){
            taker.cancel ();
            taker(action)
          }
      })
  }
  return {once,emit}
}

//export default createChannel

let channel = createChannel();
channel.once('ASYNC_ADD',function () {
  console.log('ASYNC_ADD');
});
channel.emit({type:"ASYNC_ADD"})
channel.emit({type:"ASYNC_ADD"})